<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: samples/cpp/pca.cpp</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">samples/cpp/pca.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<p>An example using PCA for dimensionality reduction while maintaining an amount of variance</p>
<div class="fragment"><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment">* pca.cpp</span></div><div class="line"><span class="comment">*</span></div><div class="line"><span class="comment">*  Author:</span></div><div class="line"><span class="comment">*  Kevin Hughes &lt;kevinhughes27[at]gmail[dot]com&gt;</span></div><div class="line"><span class="comment">*</span></div><div class="line"><span class="comment">*  Special Thanks to:</span></div><div class="line"><span class="comment">*  Philipp Wagner &lt;bytefish[at]gmx[dot]de&gt;</span></div><div class="line"><span class="comment">*</span></div><div class="line"><span class="comment">* This program demonstrates how to use OpenCV PCA with a</span></div><div class="line"><span class="comment">* specified amount of variance to retain. The effect</span></div><div class="line"><span class="comment">* is illustrated further by using a trackbar to</span></div><div class="line"><span class="comment">* change the value for retained variance.</span></div><div class="line"><span class="comment">*</span></div><div class="line"><span class="comment">* The program takes as input a text file with each line</span></div><div class="line"><span class="comment">* begin the full path to an image. PCA will be performed</span></div><div class="line"><span class="comment">* on this list of images. The author recommends using</span></div><div class="line"><span class="comment">* the first 15 faces of the AT&amp;T face data set:</span></div><div class="line"><span class="comment">* http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html</span></div><div class="line"><span class="comment">*</span></div><div class="line"><span class="comment">* so for example your input text file would look like this:</span></div><div class="line"><span class="comment">*</span></div><div class="line"><span class="comment">*        &lt;path_to_at&amp;t_faces&gt;/orl_faces/s1/1.pgm</span></div><div class="line"><span class="comment">*        &lt;path_to_at&amp;t_faces&gt;/orl_faces/s2/1.pgm</span></div><div class="line"><span class="comment">*        &lt;path_to_at&amp;t_faces&gt;/orl_faces/s3/1.pgm</span></div><div class="line"><span class="comment">*        &lt;path_to_at&amp;t_faces&gt;/orl_faces/s4/1.pgm</span></div><div class="line"><span class="comment">*        &lt;path_to_at&amp;t_faces&gt;/orl_faces/s5/1.pgm</span></div><div class="line"><span class="comment">*        &lt;path_to_at&amp;t_faces&gt;/orl_faces/s6/1.pgm</span></div><div class="line"><span class="comment">*        &lt;path_to_at&amp;t_faces&gt;/orl_faces/s7/1.pgm</span></div><div class="line"><span class="comment">*        &lt;path_to_at&amp;t_faces&gt;/orl_faces/s8/1.pgm</span></div><div class="line"><span class="comment">*        &lt;path_to_at&amp;t_faces&gt;/orl_faces/s9/1.pgm</span></div><div class="line"><span class="comment">*        &lt;path_to_at&amp;t_faces&gt;/orl_faces/s10/1.pgm</span></div><div class="line"><span class="comment">*        &lt;path_to_at&amp;t_faces&gt;/orl_faces/s11/1.pgm</span></div><div class="line"><span class="comment">*        &lt;path_to_at&amp;t_faces&gt;/orl_faces/s12/1.pgm</span></div><div class="line"><span class="comment">*        &lt;path_to_at&amp;t_faces&gt;/orl_faces/s13/1.pgm</span></div><div class="line"><span class="comment">*        &lt;path_to_at&amp;t_faces&gt;/orl_faces/s14/1.pgm</span></div><div class="line"><span class="comment">*        &lt;path_to_at&amp;t_faces&gt;/orl_faces/s15/1.pgm</span></div><div class="line"><span class="comment">*</span></div><div class="line"><span class="comment">*/</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;fstream&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;sstream&gt;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d0/d9c/core_2include_2opencv2_2core_8hpp.html">opencv2/core.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d6/d87/imgcodecs_8hpp.html">opencv2/imgcodecs.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d8/dcc/namespacestd.html">std</a>;</div><div class="line"></div><div class="line"><span class="comment">// Functions</span></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> read_imgList(<span class="keyword">const</span> <span class="keywordtype">string</span>&amp; filename, vector&lt;Mat&gt;&amp; images) {</div><div class="line">    std::ifstream file(filename.c_str(), ifstream::in);</div><div class="line">    <span class="keywordflow">if</span> (!file) {</div><div class="line">        <span class="keywordtype">string</span> error_message = <span class="stringliteral">&quot;No valid input file was given, please check the given filename.&quot;</span>;</div><div class="line">        <a name="a0"></a><a class="code" href="../../db/de0/group__core__utils.html#ga5b48c333c777666e076bd7052799f891">CV_Error</a>(<a name="a1"></a><a class="code" href="../../d1/d0d/namespacecv_1_1Error.html#a759fa1af92f7aa7377c76ffb142abccaaf587497af64537041ee62c04a92b755d">Error::StsBadArg</a>, error_message);</div><div class="line">    }</div><div class="line">    <span class="keywordtype">string</span> <a name="a2"></a><a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga7078a9fae8c7e7d13d24dac2520ae4a2">line</a>;</div><div class="line">    <span class="keywordflow">while</span> (getline(file, line)) {</div><div class="line">        images.push_back(<a name="a3"></a><a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>(line, 0));</div><div class="line">    }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span>  <a name="_a4"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> formatImagesForPCA(<span class="keyword">const</span> vector&lt;Mat&gt; &amp;data)</div><div class="line">{</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> dst(static_cast&lt;int&gt;(data.size()), data[0].rows*data[0].cols, <a name="a5"></a><a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a>);</div><div class="line">    <span class="keywordflow">for</span>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i = 0; i &lt; data.size(); i++)</div><div class="line">    {</div><div class="line">        <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> image_row = data[i].clone().reshape(1,1);</div><div class="line">        <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> row_i = dst.<a name="a6"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#a4b22e1c23af7a7f2eef8fa478cfa7434">row</a>(i);</div><div class="line">        image_row.<a name="a7"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#adf88c60c5b4980e05bb556080916978b">convertTo</a>(row_i,<a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a>);</div><div class="line">    }</div><div class="line">    <span class="keywordflow">return</span> dst;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> toGrayscale(<a name="_a8"></a><a class="code" href="../../d4/d32/classcv_1_1__InputArray.html">InputArray</a> _src) {</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> src = _src.<a name="a9"></a><a class="code" href="../../d4/d32/classcv_1_1__InputArray.html#a9c09739ca3e0ce212e2ab8374aa2f195">getMat</a>();</div><div class="line">    <span class="comment">// only allow one channel</span></div><div class="line">    <span class="keywordflow">if</span>(src.<a name="a10"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa11336b9ac538e0475d840657ce164be">channels</a>() != 1) {</div><div class="line">        <a class="code" href="../../db/de0/group__core__utils.html#ga5b48c333c777666e076bd7052799f891">CV_Error</a>(<a class="code" href="../../d1/d0d/namespacecv_1_1Error.html#a759fa1af92f7aa7377c76ffb142abccaaf587497af64537041ee62c04a92b755d">Error::StsBadArg</a>, <span class="stringliteral">&quot;Only Matrices with one channel are supported&quot;</span>);</div><div class="line">    }</div><div class="line">    <span class="comment">// create and return normalized image</span></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> dst;</div><div class="line">    <a name="a11"></a><a class="code" href="../../dc/d84/group__core__basic.html#ga1b6a396a456c8b6c6e4afd8591560d80">cv::normalize</a>(_src, dst, 0, 255, <a name="a12"></a><a class="code" href="../../d2/de8/group__core__array.html#ggad12cefbcb5291cf958a85b4b67b6149fa9f0c1c342a18114d47b516a88e29822e">NORM_MINMAX</a>, <a name="a13"></a><a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga81df635441b21f532fdace401e04f588">CV_8UC1</a>);</div><div class="line">    <span class="keywordflow">return</span> dst;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">struct </span><a name="a14"></a><a class="code" href="../../d1/dae/namespacecv_1_1gapi_1_1ie.html#a3ab1729bcaf2d08e30dd2bc645410908">params</a></div><div class="line">{</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> data;</div><div class="line">    <span class="keywordtype">int</span> ch;</div><div class="line">    <span class="keywordtype">int</span> rows;</div><div class="line">    <a name="_a15"></a><a class="code" href="../../d3/d8d/classcv_1_1PCA.html">PCA</a> pca;</div><div class="line">    <span class="keywordtype">string</span> winName;</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> onTrackbar(<span class="keywordtype">int</span> pos, <span class="keywordtype">void</span>* ptr)</div><div class="line">{</div><div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;Retained Variance = &quot;</span> &lt;&lt; pos &lt;&lt; <span class="stringliteral">&quot;%   &quot;</span>;</div><div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;re-calculating PCA...&quot;</span> &lt;&lt; std::flush;</div><div class="line"></div><div class="line">    <span class="keywordtype">double</span> var = pos / 100.0;</div><div class="line"></div><div class="line">    <span class="keyword">struct </span><a class="code" href="../../d1/dae/namespacecv_1_1gapi_1_1ie.html#a3ab1729bcaf2d08e30dd2bc645410908">params</a> *p = (<span class="keyword">struct </span><a class="code" href="../../d1/dae/namespacecv_1_1gapi_1_1ie.html#a3ab1729bcaf2d08e30dd2bc645410908">params</a> *)ptr;</div><div class="line"></div><div class="line">    p-&gt;pca = <a class="code" href="../../d3/d8d/classcv_1_1PCA.html">PCA</a>(p-&gt;data, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">cv::Mat</a>(), <a name="a16"></a><a class="code" href="../../d3/d8d/classcv_1_1PCA.html#ae8a94a2add0555b0414e85c08ff67f50a159399e962048f705645483ca16e9fd6">PCA::DATA_AS_ROW</a>, var);</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> point = p-&gt;pca.project(p-&gt;data.row(0));</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> reconstruction = p-&gt;pca.backProject(point);</div><div class="line">    reconstruction = reconstruction.<a name="a17"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#a4eb96e3251417fa88b78e2abd6cfd7d8">reshape</a>(p-&gt;ch, p-&gt;rows);</div><div class="line">    reconstruction = toGrayscale(reconstruction);</div><div class="line"></div><div class="line">    <a name="a18"></a><a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>(p-&gt;winName, reconstruction);</div><div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;done!   # of principal components: &quot;</span> &lt;&lt; p-&gt;pca.eigenvectors.rows &lt;&lt; endl;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">// Main</span></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv)</div><div class="line">{</div><div class="line">    <a name="_a19"></a><a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html">cv::CommandLineParser</a> parser(argc, argv, <span class="stringliteral">&quot;{@input||image list}{help h||show help message}&quot;</span>);</div><div class="line">    <span class="keywordflow">if</span> (parser.<a name="a20"></a><a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html#a8c41cf242b2ad8c0472e3b57893ef200">has</a>(<span class="stringliteral">&quot;help&quot;</span>))</div><div class="line">    {</div><div class="line">        parser.<a name="a21"></a><a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html#a84cda3794b765332bfe43bf8367222fb">printMessage</a>();</div><div class="line">        exit(0);</div><div class="line">    }</div><div class="line">    <span class="comment">// Get the path to your CSV.</span></div><div class="line">    <span class="keywordtype">string</span> imgList = parser.<a name="a22"></a><a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html#a190215863579a6d244dccfd261ee0224">get</a>&lt;<span class="keywordtype">string</span>&gt;(<span class="stringliteral">&quot;@input&quot;</span>);</div><div class="line">    <span class="keywordflow">if</span> (imgList.empty())</div><div class="line">    {</div><div class="line">        parser.<a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html#a84cda3794b765332bfe43bf8367222fb">printMessage</a>();</div><div class="line">        exit(1);</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="comment">// vector to hold the images</span></div><div class="line">    vector&lt;Mat&gt; images;</div><div class="line"></div><div class="line">    <span class="comment">// Read in the data. This can fail if not valid</span></div><div class="line">    <span class="keywordflow">try</span> {</div><div class="line">        read_imgList(imgList, images);</div><div class="line">    } <span class="keywordflow">catch</span> (<span class="keyword">const</span> <a name="_a23"></a><a class="code" href="../../d1/dee/classcv_1_1Exception.html">cv::Exception</a>&amp; e) {</div><div class="line">        cerr &lt;&lt; <span class="stringliteral">&quot;Error opening file \&quot;&quot;</span> &lt;&lt; imgList &lt;&lt; <span class="stringliteral">&quot;\&quot;. Reason: &quot;</span> &lt;&lt; e.<a name="a24"></a><a class="code" href="../../d1/dee/classcv_1_1Exception.html#a7ead458b4fc88ebca20b231383c49845">msg</a> &lt;&lt; endl;</div><div class="line">        exit(1);</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="comment">// Quit if there are not enough images for this demo.</span></div><div class="line">    <span class="keywordflow">if</span>(images.size() &lt;= 1) {</div><div class="line">        <span class="keywordtype">string</span> error_message = <span class="stringliteral">&quot;This demo needs at least 2 images to work. Please add more images to your data set!&quot;</span>;</div><div class="line">        <a class="code" href="../../db/de0/group__core__utils.html#ga5b48c333c777666e076bd7052799f891">CV_Error</a>(<a name="a25"></a><a class="code" href="../../d1/d0d/namespacecv_1_1Error.html#a759fa1af92f7aa7377c76ffb142abccaacf93e97abba2e7defa74fe5b99e122ac">Error::StsError</a>, error_message);</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="comment">// Reshape and stack images into a rowMatrix</span></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> data = formatImagesForPCA(images);</div><div class="line"></div><div class="line">    <span class="comment">// perform PCA</span></div><div class="line">    <a class="code" href="../../d3/d8d/classcv_1_1PCA.html">PCA</a> pca(data, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">cv::Mat</a>(), <a class="code" href="../../d3/d8d/classcv_1_1PCA.html#ae8a94a2add0555b0414e85c08ff67f50a159399e962048f705645483ca16e9fd6">PCA::DATA_AS_ROW</a>, 0.95); <span class="comment">// trackbar is initially set here, also this is a common value for retainedVariance</span></div><div class="line"></div><div class="line">    <span class="comment">// Demonstration of the effect of retainedVariance on the first image</span></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> point = pca.<a name="a26"></a><a class="code" href="../../d3/d8d/classcv_1_1PCA.html#a67c9a3f8fe804f40be58c88a3ae73f41">project</a>(data.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#a4b22e1c23af7a7f2eef8fa478cfa7434">row</a>(0)); <span class="comment">// project into the eigenspace, thus the image becomes a &quot;point&quot;</span></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> reconstruction = pca.<a name="a27"></a><a class="code" href="../../d3/d8d/classcv_1_1PCA.html#a5f84cfbdb25b9833cc1bfb5bd484ea79">backProject</a>(point); <span class="comment">// re-create the image from the &quot;point&quot;</span></div><div class="line">    reconstruction = reconstruction.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#a4eb96e3251417fa88b78e2abd6cfd7d8">reshape</a>(images[0].channels(), images[0].rows); <span class="comment">// reshape from a row vector into image shape</span></div><div class="line">    reconstruction = toGrayscale(reconstruction); <span class="comment">// re-scale for displaying purposes</span></div><div class="line"></div><div class="line">    <span class="comment">// init highgui window</span></div><div class="line">    <span class="keywordtype">string</span> winName = <span class="stringliteral">&quot;Reconstruction | press &#39;q&#39; to quit&quot;</span>;</div><div class="line">    <a name="a28"></a><a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">namedWindow</a>(winName, <a name="a29"></a><a class="code" href="../../d0/d90/group__highgui__window__flags.html#ggabf7d2c5625bc59ac130287f925557ac3a29e45c5af696f73ce5e153601e5ca0f1">WINDOW_NORMAL</a>);</div><div class="line"></div><div class="line">    <span class="comment">// params struct to pass to the trackbar handler</span></div><div class="line">    <a class="code" href="../../d1/dae/namespacecv_1_1gapi_1_1ie.html#a3ab1729bcaf2d08e30dd2bc645410908">params</a> p;</div><div class="line">    p.data = data;</div><div class="line">    p.ch = images[0].channels();</div><div class="line">    p.rows = images[0].rows;</div><div class="line">    p.pca = pca;</div><div class="line">    p.winName = winName;</div><div class="line"></div><div class="line">    <span class="comment">// create the tracbar</span></div><div class="line">    <span class="keywordtype">int</span> pos = 95;</div><div class="line">    <a name="a30"></a><a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">createTrackbar</a>(<span class="stringliteral">&quot;Retained Variance (%)&quot;</span>, winName, &amp;pos, 100, onTrackbar, (<span class="keywordtype">void</span>*)&amp;p);</div><div class="line"></div><div class="line">    <span class="comment">// display until user presses q</span></div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>(winName, reconstruction);</div><div class="line"></div><div class="line">    <span class="keywordtype">char</span> key = 0;</div><div class="line">    <span class="keywordflow">while</span>(key != <span class="charliteral">&#39;q&#39;</span>)</div><div class="line">        key = (char)<a name="a31"></a><a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>();</div><div class="line"></div><div class="line">   <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:29 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
